package org.netty.push.api;

import org.netty.push.api.dto.BaseResult;
import org.netty.push.api.dto.BatchPushRequest;
import org.netty.push.api.dto.BatchPushResponse;
import org.netty.push.api.dto.PushReponse;
import org.netty.push.api.dto.PushRequest;
import org.netty.push.api.dto.Token;
import org.netty.push.api.service.MessageService;
import org.netty.push.appinfo.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 *   消息推送服务api 
 * @author zzh
 *
 */
@Api(value="消息推送服务api",produces = "application/json")
@RestController
@RequestMapping("api/service/message/")
public class PushController {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private AppInfoService appInfoService;
	
	
	@ApiOperation(value = "获取token",httpMethod="GET" )
	@ApiImplicitParams(value = { 
			@ApiImplicitParam(name="appId",required=true,dataType="String", paramType="query" ),
			@ApiImplicitParam(name="appKey",required=true,dataType="String",paramType="query"),
			@ApiImplicitParam(name="appSecret",required=true,dataType="String",paramType="query")
			}) 
	@ApiResponses(value = { 
			@ApiResponse(code = 200, message = "提交成功",response=BaseResult.class )
			}
	)
	@GetMapping(value="token",produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
	public BaseResult<Token> getToken( String appId ,String appKey,String appSecret) {
		
		BaseResult<Token> token=new BaseResult<Token>();
		Token aToken = appInfoService.getToken(appId, appKey, appSecret);
		if(null!=aToken) {
			token.setData(aToken);
			token.setCode("200");
			token.setSuccess(true);
		}else {
			token.setCode("400");
			token.setSuccess(false);
			token.setErrormsg("获取token失败");
		}
		return token;
	}
	
	
	@ApiOperation(value = "单条消息推送",httpMethod="POST",response =PushReponse.class ,position = 102)
	@ApiImplicitParams(value = { 
			@ApiImplicitParam(name="pushRequest",required=true,dataType="PushRequest",paramType="body")
			}) 
	@ApiResponses(value = { 
			@ApiResponse(code = 200, message = "提交成功" )
			}
	)
	@PostMapping(value="singlePush",produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
	public PushReponse push(@RequestBody PushRequest pushRequest) {
		String messageId=messageService.singlePush(pushRequest);
		if(null!=messageId) {
			return PushReponse.buildSuccess(messageId);
		}
		return PushReponse.buildError("1", "error");
	}
	

	
	@ApiOperation(value = "批量消息推送",httpMethod="POST",position = 103)
	@ApiImplicitParams(value = { 
			@ApiImplicitParam(name="batchPushRequest",required=true,dataType="BatchPushRequest",paramType="body")
			}) 
	@ApiResponses(value = { 
			@ApiResponse(code = 200, message = "提交成功",response=BatchPushResponse.class )
			}
	)
	@PostMapping(value="batchPush",produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
	public BatchPushResponse batchPush(@RequestBody BatchPushRequest batchPushRequest) {
		
		return BatchPushResponse.buildError("1", "error");
	}
}
